/** Description of DTDbAdapter.java 
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.database;

import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;

public class DTDbAdapter extends AbstractDbAdapter {

	public DTDbAdapter(Context context) {
		super(context);
	}

	@Override
	public long create(ContentValues values) {
		return mDb.insert(DAYS_TRACK, null, values);
	}
	
	@Override
	public long update(long _id, ContentValues values) {
		return mDb.update(DAYS_TRACK, values, DT_PK_ID + "=" + _id, null);
	}

	@Override
	public long delete(long _id) {
		return mDb.delete(DAYS_TRACK, DT_PK_ID + "=" + _id, null);
	}

	@Override
	public Cursor fetchAll() {
		return mDb.query(DAYS_TRACK, null, null, null, null, null, null);
	}
	
	@Override
	public Cursor fetch(long _id) {
		return mDb.query(DAYS_TRACK, null, DT_PK_ID + "=" + _id, null, null, null, null);
	}
	
	@Override
	public Cursor fetchOldest() {
		return mDb.query(true, DAYS_TRACK, null, null, null, null, null, DT_ENDDATE + " ASC", "1");
	}
	
	@Override
	public Cursor fetchLatest() {
		return mDb.query(true, DAYS_TRACK, null, null, null, null, null, DT_ENDDATE + " DESC", "1");
	}

	public Cursor fetchInBetweenDate(Date startDate, Date endDate) {
		String fromDateStr = DATE_FORMAT.format(startDate);
		String toDateStr = DATE_FORMAT.format(endDate);
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		queryBuilder.setTables(DAYS_TRACK);
		return queryBuilder.query(mDb, null, DT_STARTDATE + " >= '"+ fromDateStr +"' AND "+ DT_ENDDATE + " <= '"+ toDateStr +"'", null, null, null, DT_ENDDATE + " ASC");
	}

}
